<?php

namespace Driver\Controller;

use Myself\FileSystem;

use Think\Controller;

class BaseController extends Controller {

    protected $member_id;

    protected $num=10;

    protected $userInfo;

    protected $lang;

    protected $config;

    public function _initialize()

    {

//        $this->member_id=6;

        if (I('get.token') || I('post.token')) {

            $this->userInfo=$this->checkUser();

            $this->member_id=$this->userInfo['id'] ;

        }

        else{

            echo '请使用微信扫一扫';

            exit();

        }

        $this->config=C('lang_cn');



    }

    /*用户检测session过期*/

    protected function checkUser(){

        $member = D('Driver');

        /*返回userid*/

        $result = $member->auth();

        if(!$result )

            $this->ajaxReturn(

                array(

                    'rc'=>2,

                    'msg'=>'登录失效',

                    'data'=>array(

                        'auth'=>session('AUTH_USER_STRING')

                    )

                )

            );

        return $result;

    }

    protected function checkId($id,$field=array()){

        if(!$id)

            error($this->config['parr_err']);

        if(!$info=$this->m->field($field)->find($id))

            error($this->config['parr_err']);

        return $info;

    }

    /*通用数据库 增删改 结果反馈*/

    protected function checkRes($res){

        if($res){

            success(array(),$this->config['success']);

        }else {

            error($this->config['error_update']);

        }

    }

    /*数据统计*/

    protected function statistics(){

        $m_order=M('order');

        $month=date('m');

        if($month==1 || $month==2 ||$month==3){

            $start=date('Y-01-01 00:00:00');

            $end=date("Y-03-31 23:59:59");

        }elseif($month==4 || $month==5 ||$month==6){

            $start=date('Y-04-01 00:00:00');

            $end=date("Y-06-30 23:59:59");

        }elseif($month==7 || $month==8 ||$month==9){

            $start=date('Y-07-01 00:00:00');

            $end=date("Y-09-30 23:59:59");

        }else{

            $start=date('Y-10-01 00:00:00');

            $end=date("Y-12-31 23:59:59");

        }

        $data['addtime'] = array('between',array($start,$end));

        $data['driver_id']=$this->member_id;

        //本月比上月的环比增长比例

        $now_month_count = $m_order->where(['driver_id'=>$this->member_id,'addtime'=>['like',date('Y-m').'%']])->count();

        $last_month = date("Y-m",strtotime("-1 month"));

        $last_month_count = $m_order->where(['driver_id'=>$this->member_id,'addtime'=>['like',$last_month.'%']])->count();

        $scale = bcmul(bcdiv(bcsub($now_month_count,$last_month_count),$last_month_count,2),100).'%';

        //日接单比例

        $today = date('t');

        $day_scale = bcdiv($now_month_count,$today,2);

        //客服

        $tel = M('config')->where(['key'=>'Tel'])->getField('value');

        return [

            'today'=>$m_order->where(['driver_id'=>$this->member_id,'addtime'=>['like',date('Y-m-d').'%']])->count(),

            'month'=>$m_order->where(['driver_id'=>$this->member_id,'addtime'=>['like',date('Y-m').'%']])->count(),

            'season'=>$m_order->where(['driver_id'=>$this->member_id,'addtime'=>['like',date('Y').'%']])->count(),

            'quarter'=>$m_order->where($data)->count(),

            'scale'=>$scale,

            'day_scale'=>$day_scale,

            'tell'=>$tel

        ];

    }

    /*通用文件上传办法*/

    protected function submitFile($pic,$pic_new){

        $config=C("TMPL_PARSE_STRING");//获取相关配置信息

        $picArr=explode(',',$pic_new);

        $pic_path=$picArr[1].$picArr[3]; // 实际保存数据库路径地址

        $fromPath=$picArr[0].$picArr[2];//源文件夹

        $ToPath=$picArr[0].$picArr[1];//目标文件夹

        $file[]=$picArr[3];

        $file[]="thumb_".$picArr[3];

        FileSystem::mv($file , $fromPath , $ToPath , 'k');

        $delpic=__PHYSICS__.'/Upload/'.$pic;

        FileSystem::rm($delpic);//删除原图

        return $pic_path;

    }

    /*文件删除*/

    protected function baseDelFile($m,$ids,$field){

        $file = new \Myself\FileSystem();// 实例化上传类

        $config=C("TMPL_PARSE_STRING");//获取相关配置信息

        $pInfo = M($m)->where('id in('.$ids.')')->field($field)->select();

        foreach($pInfo as $item){

            $delpic[]=$config["__AFILE__"].$item[$field];

            $info=pathinfo($item[$field]);

            $delpic[]=$config["__AFILE__"].$info['dirname']."/thumb_".$info["basename"];

        }

        $file->rm($delpic);//删除文件

        return $delpic;

    }



}